<?php
namespace app\tools\logic;
use think\facade\Db;

class ChannelCountryLogic{
	static function toContinent(){
		$continent = input('continent', 0);
		$relation_type = input('relation_type', 1);
		if($relation_type=="1"){
			if(!$continent){
				return 139;
			}
		}
		
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}

		foreach($ids as $v){
			if($relation_type==="0"){
				$continent=0;
			}
			Db::name('channel_country')->where('id', $v)->update(['continent'=>$continent]);
		}
		return 0;
	}


	static function getList(){
		$channel_id = input('channel_id', 0);	//通道ID
		$page_size = input('page_size', 10);	//每页数量
		$country_name = input('country_name', '');	//国家名字
		
		$continent = input('continent', '');    //属于哪个洲  1，2，3，4，5
		$type = input('type', 0);
		$is_relation = input('is_relation','1');
		
		if(!$type){
			//普通获取国家列表
			$obj = Db::name('channel_country')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127);
			$country_name && $obj->where('c.country_name|c.country_zh','like','%'.$country_name.'%');
			$obj->where('c.channel_id', $channel_id);
			$list = $obj->order('c.sort asc,c.id desc')
				->paginate($page_size);
		}else{
			//根据通道，洲获取国家列表，可以获取所有国家
			$obj = Db::name('channel_country')
				->alias('c')
				->field('c.id,c.continent,dd.name as continent_name,c.country_name,c.country_zh,
						 c.country_en,cc.channel_name')
				->join('dict_data dd','dd.value=c.continent','left')
				->join('channel cc','cc.id=c.channel_id','left')
				->where('c.status','<>',127);
			if($channel_id!=0){
				$obj->where('c.channel_id', $channel_id);
			}

			if($is_relation=="1"){
				$obj->where('dd.id','not null');
				//只有已归属才需要这个参数
				if($continent!=''){
					//echo "ABCD";
					$obj->where('c.continent', $continent);
				}
			}else{
				$obj->where('dd.id','null');
			}
			$list = $obj->order('c.channel_id asc,c.id desc')
				->paginate($page_size);
		}
				
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('channel_country')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('c.id', $id)
				->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['channel_id'] = input('channel_id','');
		if(!$data['channel_id']){
			return 125;
		}

		$info = Db::name('channel')->where('id', $data['channel_id'])->find();
		if(!$info){
			return 128;
		}
		$base_rate = input('base_rate', 0);
		if(!$base_rate){
			$data['base_rate'] = $info['base_rate']?:2;
		}else{
			$data['base_rate'] = $base_rate;
		}

		$data['status'] = input('status', 1);
		$data['country_ico'] = input('country_ico', "");
		$data['country_id'] = input('country_id', 0);
		$data['country_name'] = input('country_name', '');
		$data['country_zh'] = input('country_zh','');
		$data['country_en'] = input('country_en','');
		$data['country_code'] = input('country_code', '');
		$data['sort'] = input('sort', 0);

		$gj = Db::name('channel_country')
				->field('id')
				->where('channel_id', $data['channel_id'])
				->where('country_code', $data['country_code'])
				->where('status','<>',127)
				->where('id','<>',$id)
				->find();
		if($gj){
			return 130;
		}

		$rs = Db::name('channel_country')->where('id', $id)->update($data);
		if($rs!==false){
			//编辑国家，是否需要同步所有关联得服务呢
			Db::name('channel_country_server')
				->where('channel_country_id', $id)
				->update(['base_rate'=>$data['base_rate']]);

			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['channel_id'] = input('channel_id','');
		if(!$data['channel_id']){
			return 125;
		}

		$info = Db::name('channel')->where('id', $data['channel_id'])->find();
		if(!$info){
			return 128;
		}
		$base_rate = input('base_rate', 0);
		if(!$base_rate){
			$data['base_rate'] = $info['base_rate']?:2;
		}else{
			$data['base_rate'] = $base_rate;
		}

		$data['status'] = input('status', 1);
		$data['country_ico'] = input('country_ico', "");
		$data['country_id'] = input('country_id', 0);
		$data['country_name'] = input('country_name', '');
		$data['country_zh'] = input('country_zh','');
		$data['country_en'] = input('country_en','');
		$data['country_code'] = input('country_code', '');
		$data['sort'] = input('sort', 0);
		$data['create_time'] = date('Y-m-d H:i:s');

		//国家是否已经存在
		$gj = Db::name('channel_country')
				->field('id')
				->where('channel_id', $data['channel_id'])
				->where('country_code', $data['country_code'])
				->where('status','<>',127)
				->find();
		if($gj){
			return 130;
		}

		$id = Db::name('channel_country')->insertGetId($data);
		if($id!==false){
			//国家新增的时候去关联服务
			self::relateServer($data['channel_id'], $id, $data['base_rate']);
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('channel_country')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function addList(){
		$channel_id = input('channel_id', 0);
		if(!$channel_id){
			return ['code'=>125];
		}
		$info = Db::name('channel')->where('id', $channel_id)->find();
		if(!$info){
			return ['code'=>128];
		}
		//通道修改为启用状态
		Db::name('channel')->where('id', $channel_id)->update(['status'=>1]);

		$item = input('item',[]);
		if(!$item){
			return ['code'=>126];
		}

		$cf = 0;
		$cg = 0;
		//批量新增
		foreach($item as $k=>$v){
			$v = trim($v,'-');
			$acc = array_values(array_filter(explode('@@', $v)));
			$data = [];
			$data['channel_id'] = $channel_id;
			$data['country_ico'] = $acc[0];
			
			//缺少国家代码
			if(isset($acc[1])){
				$data['country_code'] = $acc[1];
			}else{
				$cf++;
				continue;
			}
			//缺少国家名称
			if(isset($acc[2])){
				$data['country_name'] = $acc[2];
			}else{
				$cf++;
				continue;
			}
			
			if(isset($acc[3])){
				$data['country_zh'] = $acc[3];
			}

			if(isset($acc[4])){
				$data['country_en'] = $acc[4];
			}

			if(isset($acc[5])){
				$data['base_rate'] = $acc[5];
			}else{
				//默认直接给两倍
				$data['base_rate'] = $info['base_rate']?:3;
			}

			if(isset($acc[6])){
				$data['channel_type'] = $acc[6];
			}else{
				$data['channel_type'] = 0;
			}
			
			$data['status'] = 1;
			$data['sort'] = 50;
			$data['create_time'] = date('Y-m-d H:i:s');
			$info = Db::name('channel_country')
					->where('channel_id', $channel_id)
					->where('country_code', $data['country_code'])
					->find();

			if($info){
				Db::name('channel_country')->where('id', $info['id'])->update($data);
			}else{
				$id = Db::name('channel_country')->insertGetId($data);
				self::relateServer($channel_id, $id, $data['base_rate']);
			}
			$cg++;
			
		}
		return ['code'=>0,'cf'=>$cf,'cg'=>$cg];
		
	}


	//关联本通道的所有服务
	static function relateServer($channel_id=0, $country_id=0, $base_rate=2){
		if(!$channel_id || !$country_id){
			return;
		}

		$serverList = Db::name('channel_server')
						->field('id,base_rate,status')
						->where('channel_id', $channel_id)
						->where('status', 1)
						->select()->toArray();
		if($serverList){
			foreach($serverList as $k=>$v){
				$data = [];
				$data['channel_country_id'] = $country_id;
				$data['channel_server_id'] = $v['id'];
				$data['base_rate'] = $base_rate;
				Db::name('channel_country_server')->insert($data);
			}
		}
	}

	//国家关联服务
	static function server(){
		$id = input('id','');
		$checked = input('checked', "1");
		$server_name = input('server_name','');
		$page_size = input('page_size', 10);
		if(!$id){
			return ['code'=>110];
		}
		$info = Db::name('channel_country')
				->where('id', $id)
				->find();
		if(!$info){
			return ['code'=>131];
		}
		$obj = Db::name('channel_server')
				->alias('cs')
				->field("cs.id,
						 cs.channel_id,
						 cs.server_ico,
						 cs.server_name,
						 cs.server_zh,
						 cs.server_code,
						 IFNULL(ccs.base_rate, cs.base_rate) as base_rate,
						 case 
						 	when ccs.channel_country_id>0  then 'true' 
						 	else 'false' end as has_check")
				->join('channel_country_server ccs','ccs.channel_server_id=cs.id and ccs.channel_country_id='.$id,'left')
				->where('cs.status',1);
		if($checked==="1"){
			$obj->where('ccs.channel_country_id','not null');
		}elseif($checked==="0"){
			$obj->where('ccs.channel_country_id','null');
		}

		if($server_name){
			$obj->where('cs.server_zh|cs.server_name','like','%'.$server_name.'%');
			// $obj->where('cs.server_zh','like','%'.$server_name.'%');
			// echo "咋咋"; 
		}

		$list = $obj->where('cs.channel_id', $info['channel_id'])
				->order('cs.sort asc, cs.id asc')
				->paginate($page_size);
		return ['code'=>0,'data'=>$list];

	}


	static function serverToCountry(){
		$id = input('id','');
		$relation_type = input('relation_type', 1);
		if(!$id){
			return 110;
		}
		$info = Db::name('channel_country')
				->where('id', $id)
				->find();
		if(!$info){
			return 131;
		}

		$server_ids = input('server_ids', "");
		$ids = explode(',', $server_ids);
		if(!$ids){
			return 140;
		}
		//先删除国家的所有记录
		// Db::name('channel_country_server')
		// 	->where('channel_country_id',$id)
		// 	->delete();
		//循环添加
		foreach($ids as $sid){
			$serverInfo = Db::name('channel_server')->where('id', $sid)->find();
			// print_r($serverInfo);
			if($relation_type==1){
				//绑定关系
				$data = [];
				$data['channel_country_id'] = $id;
				$data['channel_server_id'] = $sid;
				$data['base_rate'] = $serverInfo['base_rate'];
				$csInfo = Db::name('channel_country_server')
							->where('channel_country_id', $id)
							->where('channel_server_id', $sid)
							->find();
				if($csInfo){
					continue;
				}
				$data['base_rate'] = $info['base_rate'];
				$data['channel_id'] = $serverInfo['channel_id'];
				Db::name('channel_country_server')->insert($data);
			}else{
				//解除关系
				$data = [];
				$data['channel_country_id'] = $id;
				$data['channel_server_id'] = $sid;
				// $data['base_rate'] = $serverInfo['base_rate'];
				// $data['channel_id'] = $serverInfo['channel_id'];
				Db::name('channel_country_server')
					->where('channel_country_id', $id)
					->where('channel_server_id', $sid)
					->delete();
			}
		}
		return 0;
	}

	static function saveServer(){
		$country_id = input('country_id', '');
		$server_id = input('server_id', '');
		$base_rate = input('base_rate', 2);
		if(!$country_id){
			return 150;
		}

		if(!$server_id){
			return 151;
		}
		$server_ids = explode(',', $server_id);
		$info = Db::name('channel_country')->where('id', $country_id)->find();

		$rs = Db::name('channel_country_server')
				->where('channel_country_id', $country_id)
				->where('channel_server_id', 'in', $server_ids)
				->update(['base_rate'=>$base_rate,'channel_id'=>$info['channel_id']]);
		if(false!==$rs){
			return 0;
		}else{
			return 152;
		}
	}
}